package com.example.ayniu.treadmill;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ayniu.treadmill.base.BaseActivity;
import com.example.ayniu.treadmill.util.UserInfoUtil;

public class SetPhoneActivity extends BaseActivity {

    private Button bt_setPhone;
    private TextView tv_getverification;
    private EditText et_phone,et_verification;
    private CountDownTimer mCountDownTimer;

    //计算Toast的出现时间
    private long firstTimeOfToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_phone);

        initView();
    }

    private void initView(){
        et_phone = (EditText) findViewById(R.id.et_setphone);
        et_verification = (EditText) findViewById(R.id.et_verification);

        bt_setPhone = (Button) findViewById(R.id.bt_setphone);
        bt_setPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //验证请求绑定的手机号码
                verifyPhone(et_phone.getText().toString(),et_verification.getText().toString());
            }
        });
        tv_getverification = (TextView) findViewById(R.id.tv_getverification);
        tv_getverification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_getverification.setBackgroundResource(R.color.main);
                mCountDownTimer.start();
            }
        });

        //获取验证码倒计时的实现
        mCountDownTimer = new CountDownTimer(61000, 1000) {
            int i = 60;
            @Override
            public void onTick(long millisUntilFinished) {
                tv_getverification.setText("（"+i + "秒）");
                i--;
            }

            @Override
            public void onFinish() {
                i = 60;
                tv_getverification.setBackgroundResource(R.drawable.bt_getverigication);
                tv_getverification.setText("");
                mCountDownTimer.cancel();
            }
        };
    }

    private void verifyPhone(String phoneNum, String verification){
        if(phoneNum.equals("")){
            showToast("请输入验证手机号码");
        }else if(verification.equals("")){
            showToast("验证码不能为空");
        }else{
            //网络请求验证

            //将手机保存到本地，下次使用app获取本地信息即可
            UserInfoUtil.setLocalPara("USER_PHONE", phoneNum);
            Intent intent = new Intent(SetPhoneActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void showToast(String str){
        // Toast的刷新时间为3s
        long secondTimeOfToast = System.currentTimeMillis();
        if (secondTimeOfToast - firstTimeOfToast > 3000) {
            Toast.makeText(SetPhoneActivity.this, str, Toast.LENGTH_SHORT).show();
            firstTimeOfToast = secondTimeOfToast;
        }
    }
}
